package com.qianniu.napi.admin.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.google.common.base.Strings;
import com.qianniu.napi.admin.entity.Goods;
import com.qianniu.napi.admin.entity.Order;
import com.qianniu.napi.admin.mapper.GoodsMapper;
import com.qianniu.napi.admin.service.IGoodsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品信息 服务实现类
 * </p>
 *
 * @author chenxuebin
 * @since 2018-06-23
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements IGoodsService {

    private static final Logger logger = LoggerFactory.getLogger(GoodsServiceImpl.class);

    @Autowired
    private GoodsMapper goodsMapper;

    public List<Goods> getGoodsByMap(Map map) {
        return goodsMapper.getBaseGoodsByMap(map);
    }

    public List<Goods> getYxgoodsList(int pageIndex, int pageSize, Double lat, Double lng, Long mpid, String hytype, String yxtype, String shicode,String status, String orderby) {
        List<Goods> list = new ArrayList<Goods>();
        Map<String, Object> map = new HashMap<String, Object>();

        map.put("mpid", mpid);
        if (!Strings.isNullOrEmpty(shicode)) map.put("shicode", shicode);
        if (!Strings.isNullOrEmpty(yxtype)) map.put("yxtype", yxtype);
        if (!Strings.isNullOrEmpty(hytype)) map.put("hytype", hytype);
        if (!Strings.isNullOrEmpty(status)) map.put("status", status);

        logger.debug("====================getYxgoodsList map=" + JSON.toJSONString(map));

        int total = goodsMapper.getYxgoodsCount(map);

        if (total == 0) return null;
        int skip = (pageIndex - 1) * pageSize;
        if (Strings.isNullOrEmpty(orderby)) orderby = "distance";

        logger.debug("==========getYxgoodsList 111");

        map.put("lat", lat);
        map.put("lng", lng);
        map.put("skip", skip);
        map.put("size", pageSize);
        map.put("orderby", orderby);
        map.put("status", status);
        String sort = "desc";
        if (!Strings.isNullOrEmpty(orderby) && !orderby.equals("distance")) {
            sort = "asc";
        }
        map.put("sort", sort);
        logger.debug("==========getYxgoodsList map=" + JSON.toJSONString(map));
        list = goodsMapper.getYxgoodsList(map);

        return list;
    }

    public List<Goods> getYxgoodslmList(int pageIndex, int pageSize, Double lat, Double lng, Long mpid, String hytype, String yxtype, String shicode, String orderby) {
        List<Goods> list = new ArrayList<Goods>();
        Map<String, Object> map = new HashMap<String, Object>();

        map.put("mpid", mpid);
        if (!Strings.isNullOrEmpty(shicode)) map.put("shicode", shicode);
        if (!Strings.isNullOrEmpty(yxtype)) map.put("yxtype", yxtype);
        if (!Strings.isNullOrEmpty(hytype)) map.put("hytype", hytype);

        map.put("lat", lat);
        map.put("lng", lng);

        logger.debug("====================getYxgoodsList map=" + JSON.toJSONString(map));

        int total = goodsMapper.getYxgoodslmCount(map);

        if (total == 0) return null;
        int skip = (pageIndex - 1) * pageSize;
        if (Strings.isNullOrEmpty(orderby)) orderby = "distance";

        logger.debug("==========getYxgoodsList 111");

        map.put("skip", skip);
        map.put("size", pageSize);
        map.put("orderby", orderby);
        String sort = "desc";
        if (!Strings.isNullOrEmpty(orderby) && !orderby.equals("distance")) {
            sort = "asc";
        }
        map.put("sort", sort);
        logger.debug("==========getYxgoodsList map=" + JSON.toJSONString(map));
        list = goodsMapper.getYxgoodslmList(map);

        return list;
    }

    public List<Goods> getYxgoodsList2(int pageIndex, int pageSize, Double lat, Double lng, Long mpid, String hytype, String yxtype, String shicode, String orderby) {
        List<Goods> list = new ArrayList<Goods>();
        Map<String, Object> map = new HashMap<String, Object>();

        map.put("mpid", mpid);
        if (!Strings.isNullOrEmpty(shicode)) map.put("shicode", shicode);
        if (!Strings.isNullOrEmpty(yxtype)) map.put("yxtype", yxtype);
        if (!Strings.isNullOrEmpty(hytype)) map.put("hytype", hytype);

        logger.debug("====================getYxgoodsList map=" + JSON.toJSONString(map));

        int total = goodsMapper.getYxgoodsCount2(map);

        if (total == 0) return null;
        int skip = (pageIndex - 1) * pageSize;
        if (Strings.isNullOrEmpty(orderby)) orderby = "distance";

        logger.debug("==========getYxgoodsList 111");

        map.put("lat", lat);
        map.put("lng", lng);
        map.put("skip", skip);
        map.put("size", pageSize);
        map.put("orderby", orderby);
        String sort = "desc";
        if (!Strings.isNullOrEmpty(orderby) && !orderby.equals("distance")) {
            sort = "asc";
        }
        map.put("sort", sort);
        logger.debug("==========getYxgoodsList map=" + JSON.toJSONString(map));
        list = goodsMapper.getYxgoodsList2(map);

        return list;
    }

    //------------投票相关计算方法-------------
    public List<Order> calcLastpiao(List<Order> list) {
        if (list == null || list.size() == 0) return list;
        Order last = null;
        for (int i = 0; i < list.size(); i++) {
            if (i == 0) {
                list.get(i).setLastpiao(0);
            } else {
                list.get(i).setLastpiao(last.getGoodsNumber() - list.get(i).getGoodsNumber());
            }
            list.get(i).setPaiming(i + 1);
            last = list.get(i);
        }
        return list;
    }

    public Map<String, Integer> calcLastpiaoMap(List<Order> list) {
        if (list == null || list.size() == 0) return null;
        Map<String, Integer> map = new HashMap<String, Integer>();
        for (int i = 0; i < list.size(); i++) {
            map.put("lastpiao" + list.get(i).getId(), list.get(i).getLastpiao());
        }
        return map;
    }

    public List<Order> setLastpiaoMap(List<Order> list, Map<String, Integer> map) {
        if (list == null || list.size() == 0) return list;
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setLastpiao(map.get("lastpiao" + list.get(i).getId()));
        }
        return list;
    }

    public Map<String, Integer> calcPaiming(List<Order> list) {
        if (list == null || list.size() == 0) return null;
        Map<String, Integer> map = new HashMap<String, Integer>();
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setPaiming(i + 1);
            map.put("paiming" + list.get(i).getId(), list.get(i).getPaiming());
        }
        return map;
    }

    public List<Order> setPaiming(List<Order> list, Map<String, Integer> map) {
        if (list == null || list.size() == 0) return list;
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setPaiming(map.get("paiming" + list.get(i).getId()));
        }
        return list;
    }

    public Map<String, Order> setlppmMap(List<Order> list) {
        if (list == null || list.size() == 0) return null;
        Map<String, Order> map = new HashMap<String, Order>();
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setPaiming(i + 1);
            map.put("lppm" + list.get(i).getId(), list.get(i));
        }
        return map;
    }

    public String replaceXing(String phone) {
        if (Strings.isNullOrEmpty(phone)) return phone;
        if (phone.length() < 11) return phone;
        String p1 = phone.substring(0, 3);
        String p2 = "****";
        String p3 = phone.substring(7);
        return p1 + p2 + p3;
    }

    public String replaceMiddle(String name) {
        if (Strings.isNullOrEmpty(name)) return name;
        if (name.length() == 1) return name;
        else if (name.length() == 2) return name.substring(0, 1) + "*";
        else {
            String result = name.substring(0, 1);
            for (int i = 1; i < name.length() - 1; i++) {
                result += "*";
            }
            result += name.substring(name.length() - 1);
            return result;
        }
    }

    //------------投票相关计算方法End-------------


    public List<Goods> getYxbkgoodsList(int pageIndex, int pageSize, Double lat, Double lng, Long mpid, String hytype, String yxtype, String shicode, String orderby) {
        List<Goods> list = new ArrayList<Goods>();
        Map<String, Object> map = new HashMap<String, Object>();

        map.put("mpid", mpid);
        if (!Strings.isNullOrEmpty(shicode)) map.put("shicode", shicode);
        if (!Strings.isNullOrEmpty(yxtype)) map.put("yxtype", yxtype);
        if (!Strings.isNullOrEmpty(hytype)) map.put("hytype", hytype);

        logger.debug("====================getYxgoodsList map=" + JSON.toJSONString(map));

        map.put("lat", lat);
        map.put("lng", lng);

        int total = goodsMapper.getYxbkgoodsCount(map);

        if (total == 0) return null;
        int skip = (pageIndex - 1) * pageSize;
        if (Strings.isNullOrEmpty(orderby)) orderby = "distance";

        map.put("skip", skip);
        map.put("size", pageSize);
        map.put("orderby", orderby);
        String sort = "desc";
        if (!Strings.isNullOrEmpty(orderby) && !orderby.equals("distance")) {
            sort = "asc";
        }
        map.put("sort", sort);
        logger.debug("==========getYxgoodsList map=" + JSON.toJSONString(map));
        list = goodsMapper.getYxbkgoodsList(map);

        return list;
    }


    public List<Goods> getYxgoodsList3(int pageIndex, int pageSize, Double lat, Double lng, Long mpid, String hytype, String yxtype, String shicode,String status, String orderby) {
        List<Goods> list = new ArrayList<Goods>();
        Map<String, Object> map = new HashMap<String, Object>();

        map.put("mpid", mpid);
        if (!Strings.isNullOrEmpty(shicode)) map.put("shicode", shicode);
        if (!Strings.isNullOrEmpty(yxtype)) map.put("yxtype", yxtype);
        if (!Strings.isNullOrEmpty(hytype)) map.put("hytype", hytype);
        if (!Strings.isNullOrEmpty(status)) map.put("status", status);

        logger.debug("====================getYxgoodsList map=" + JSON.toJSONString(map));

        int total = goodsMapper.getYxgoodsCount(map);

        if (total == 0) return null;
        int skip = (pageIndex - 1) * pageSize;
        if (Strings.isNullOrEmpty(orderby)) orderby = "distance";

        logger.debug("==========getYxgoodsList 111");

        map.put("lat", lat);
        map.put("lng", lng);
        map.put("skip", skip);
        map.put("size", pageSize);
        map.put("orderby", orderby);
        map.put("status", status);
        map.put("type", "3938");
        String sort = "desc";
        if (!Strings.isNullOrEmpty(orderby) && !orderby.equals("distance")) {
            sort = "asc";
        }
        map.put("sort", sort);
        logger.debug("==========getYxgoodsList map=" + JSON.toJSONString(map));
        list = goodsMapper.getYxgoodsList(map);

        return list;
    }

}
